package uz.rt.plan;


import uz.rt.utils.Itemizable;

import java.util.ArrayList;
import java.util.Iterator;

public class Plan implements Itemizable<Integer, Beam>
{
    protected ArrayList<Beam> beams = null;

    Plan()
    {
        beams = new ArrayList<Beam>();
    }

    Plan(final ArrayList<Beam> list)
    {
        if (list != null) beams = list;
        else beams = new ArrayList<Beam>();
    }

    public void add(Beam beam)
    {
        beams.add(beam);
    }

    public void edit(Integer idx, Beam beam)
    {
        if (idx < 0) throw new ArrayIndexOutOfBoundsException("Cannot edit coordinate : index out of bound (<0)");
        else if (idx > beams.size() - 1)
            throw new ArrayIndexOutOfBoundsException("Cannot edit coordinate : index out of bound (>dimension)");
        else beams.set(idx, beam);
    }

    public void removeByKey(Integer idx)
    {
        if (idx < 0) throw new ArrayIndexOutOfBoundsException("Cannot edit coordinate : index out of bound (<0)");
        else if (idx > beams.size() - 1)
            throw new ArrayIndexOutOfBoundsException("Cannot edit coordinate : index out of bound (>dimension)");
        beams.remove(idx.intValue());
    }

    public void removeValue(Beam beam)
    {
        Iterator<Beam> itr = beams.iterator();
        while (itr.hasNext())
        {
            Beam b = itr.next();
            if (b == beam) itr.remove();
        }
    }

    public int size()
    {
        return beams.size();
    }
}
